$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'water/meterarchives/list',
        datatype: "json",
        colModel: [			
			{ label: 'unkey', name: 'unkey', index: 'unkey', width: 50, key: true,hidden:true },
			{ label: '营业公司', name: 'areaId', index: 'area_id', width: 80 },
			{ label: '营业所', name: 'unitWId', index: 'unit_w_id', width: 80 },
			{ label: '物业小区', name: 'proName', index: 'pro_name', width: 80 },
			{ label: '客户编号', name: 'clientId', index: 'client_id', width: 80 },
			{ label: '册本号', name: 'clientNo', index: 'client_no', width: 80 },
			{ label: '抄表员', name: 'staffName', index: 'staff_name', width: 80 },
			{ label: '表卡名称', name: 'meterCardTitle', index: 'meter_card_title', width: 80 },
			{ label: '表卡地址', name: 'meterCardAddress', index: 'meter_card_address', width: 80 },
			{ label: '水表厂家', name: 'meterCompany', index: 'meter_company', width: 80 },
			{ label: '水表口径', name: 'meterCaliber', index: 'meter_caliber', width: 80 },
			{ label: '表号', name: 'meterId', index: 'meter_id', width: 80 },
			{ label: '表封号', name: 'meterSealNo', index: 'meter_seal_no', width: 80 },
			{ label: '管径', name: 'pipeDiameter', index: 'pipe_diameter', width: 80 },
			{ label: '表位置', name: 'meterPosition', index: 'meter_position', width: 80 },
			{ label: '水表型号', name: 'meterType', index: 'meter_type', width: 80 },
			{ label: '通讯协议', name: 'meterProtocol', index: 'meter_protocol', width: 80 },
			{ label: '装换表日期', name: 'changeDate', index: 'change_date', width: 80 },
			{ label: '用水性质', name: 'waterCharacter', index: 'water_character', width: 80 },
			{ label: '用户类型', name: 'clientType', index: 'client_type', width: 80 },
			{ label: '移动电话', name: 'mobileNo', index: 'mobile_no', width: 80 },
			{ label: '表号变更次数', name: 'meterIdChanges', index: 'meter_id_changes', width: 80 },
			{ label: '表封号号变更次数', name: 'meterSealNoChanges', index: 'meter_seal_no_changes', width: 80 },
			/*{ label: '', name: 'desc1', index: 'desc1', width: 80 },
			{ label: '', name: 'desc2', index: 'desc2', width: 80 },
			{ label: '', name: 'desc3', index: 'desc3', width: 80 },
			{ label: '', name: 'desc4', index: 'desc4', width: 80 },
			{ label: '', name: 'desc5', index: 'desc5', width: 80 },
			{ label: '', name: 'desc6', index: 'desc6', width: 80 },
			{ label: '', name: 'desc7', index: 'desc7', width: 80 },
			{ label: '', name: 'desc8', index: 'desc8', width: 80 },
			{ label: '', name: 'desc9', index: 'desc9', width: 80 },
			{ label: '', name: 'desc10', index: 'desc10', width: 80 }	*/
        ],
		viewrecords: true,
        height: window.innerHeight-180 > 385 ? window.innerHeight-180 : 385,
        rowNum: 50,
		rowList : [10,30,50,100,200,250,300,500],
        rownumbers: true, 
        rownumWidth: 40,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

	$(function () {
		$('#inputClear').click(function () {
			$('input')[0].value = "";
			vm.q.value="";
		})
	});

	new AjaxUpload('#upload', {
		action: baseURL + "water/meterarchives/importExcel",
		name: 'myfile',
		autoSubmit:true,
		responseType:"json",
		onSubmit:function(file, extension){
			if (!(extension && /^(xls|xlsx)$/.test(extension.toLowerCase()))){
				alert('只支持xls和xlsx格式文件！');
				return false;
			}
		},
		onComplete : function(file, r){
			if(r.code == 0){
				layer.msg("文件【"+r.url+"】导入完毕！", {icon: 1});
				vm.query();
			}else{
				alert(r.msg);
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			key: '',
			value:null,
			dept:null,
			ckey: '',
		},
		newpage : false,

		searchKeyDictInfo: {},
		showList: true,
		title: null,
		meterArchives: {}
	},
	methods: {
		query: function () {
			if(vm.q.value != null && vm.q.value != ""
				&& (vm.q.key == null || vm.q.key == "")){
				alert('请选择查询条件!');
				return;
			}
			vm.newpage=true;

			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.meterArchives = {};
		},
		update: function (event) {
			var unkey = getSelectedRow();
			if(unkey == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(unkey)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.meterArchives.unkey == null ? "water/meterarchives/save" : "water/meterarchives/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.meterArchives),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var unkeys = getSelectedRows();
			if(unkeys == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "water/meterarchives/delete",
                        contentType: "application/json",
                        data: JSON.stringify(unkeys),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getSearchKeyDict: function(){
			$.get(baseURL + "sys/dict/list",{name:"qkey_meterarchives"}, function(r){
				vm.searchKeyDictInfo = r.page.list;
			});
		},
		getInfo: function(unkey){
			$.get(baseURL + "water/meterarchives/info/"+unkey, function(r){
                vm.meterArchives = r.meterArchives;
            });
		},
		outxls:function (){
			var total = $("#jqGrid").jqGrid('getGridParam', 'records') + "";
			var dept = vm.q.dept==null ? "" : vm.q.dept+  "";
			var url = baseURL
				+ "water/meterarchives/export?params="
				+ encodeURIComponent(JSON.stringify({key:vm.q.key,value:vm.q.value,dept:dept,limit:total,page:"1"}));

			//console.log(decodeURIComponent(url));
			location.href = url;
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'key': vm.q.key,'value':vm.q.value,'dept':vm.q.dept},
				page:page
            }).trigger("reloadGrid");
		}
	},
	created: function(){
		//this.getDeptTreeLeft();
		this.getSearchKeyDict();
	}
});